﻿var loginid,loginuser,loginname,loginkey;
var layer,element,form;
layui.config({
	version:'1510164124181'
}).use(['form', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
    element = layui.element; //元素操作
	loginid = $.session.get("userid");
	loginuser = $.session.get("admin_realname");	
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	//console.log("sdfd");
	searwidth()
});
var count = 1;
//加载判断大类型个数并设置宽度
function searwidth(){	   
    var sd=$(".ui-sortable").children(".box-child").length;
    if(sd<=2){ 
		$(".ui-sortable").append('<div class="box-child sub-mueu-plusall"><div class="sub-js-sortable sub-mueu-plus text-center" onclick="sub_mueu_plus(this)"><i class="icon-plus" ></i>添加菜单</div></div>');  
		if(sd==0){
		   $(".ui-sortable").children(".box-child").width(100+"%");
		}else{
		   $(".ui-sortable").children(".box-child").width(100/sd+"%");      	
		}
	  
	}
}  

//新增大类型并从新定义大类型宽度
function sub_mueu_plus(n){	
  var sf=$(".ui-sortable").children(".box-child").length;	 
  var len = count++;
  if(sf<=2){      	      
	$(n).parent(".box-child").before('<div class="box-child" ><div onclick="remove_box(this,2)" class="remove_box_title menunum" data-id="'+len+'" data-pid="0"><i class="icon-globe"></i>菜单名称('+len+'==0)</div><div class="sub-pre-menu-box"><div class="sub-js-sortable sub-js-plus" onclick="sub_js_plus(this)"><i class="icon-plus" ></i></div></div></div>');        
	$(".ui-sortable").children(".box-child").width(100/sf+"%");      
  }else if(sf==3){
	 $(n).parent(".box-child").before('<div class="box-child"><div onclick="remove_box(this,2)" class="remove_box_title menunum" data-id="'+len+'" data-pid="0"><i class="icon-globe"></i>菜单名称('+len+'==0)</div><div class="sub-pre-menu-box"><div class="sub-js-sortable sub-js-plus" onclick="sub_js_plus(this)"><i class="icon-plus" ></i></div></div></div>');        
	 $(".ui-sortable").children(".box-child").width(100/sf+"%");
	 $(".ui-sortable").children(".sub-mueu-plusall").remove();
  }
 
};
 
//添加子栏目
function sub_js_plus(b){	 
   $(".ui-sortable").find("div").removeClass("sub-on-item");
   var numbers=$(b).siblings().length;
   var pid = $(b).parent().siblings(".remove_box_title").attr("data-id");
   var len = count++;
   $(b).parent().siblings(".remove_box_title").attr("menuurl","");
   $(b).before('<div class="sub-js-sortable sub-js-item sub-on-item menunum" onclick="remove_box(this,1)" data-id="'+len+'" data-pid="'+pid+'">子菜单名称('+len+'=='+pid+')</div>');	    	
   if(numbers>=4){
	 $(b).remove();
   }    	      	 
	  
}

//给各个栏目设置当前选中的栏目
function remove_box(w,ty){
	$(w).parents(".ui-sortable").find("div").removeClass("sub-on-item");
	$(w).addClass("sub-on-item");//remove_box
	var len = $(w).siblings(".sub-pre-menu-box").children(".sub-js-item").length;
	if(len>0){//如果有子菜单则清除父级菜单的url
		$(".ishowurl").hide();
		$(w).attr("menuurl","");
	}else{
		$(".ishowurl").show();
		
	}
	$("#menuname").val(($(w).attr("menuname")==undefined?'':$(w).attr("menuname")));//获取菜单名字
    $("#menuurl").val(($(w).attr("menuurl")==undefined?'':$(w).attr("menuurl")));//获取菜单跳转链接
}

//根据选择的栏目进行删除
$(".remove_meun").click(function(){
	if($(".sub-on-item").length>0){
		var q=$(".ui-sortable").find(".sub-on-item").siblings(".sub-js-item").length;
		var parentname=$(".ui-sortable").find(".sub-on-item").parent().hasClass("sub-pre-menu-box");
		if(parentname==true){//二级
			if(q==4){
				$(".ui-sortable").find(".sub-on-item").parent(".sub-pre-menu-box").append('<div class="sub-js-sortable sub-js-plus" onclick="sub_js_plus(this)"><i class="icon-plus"></i></div>');
				$(".ui-sortable").find(".sub-on-item").remove();
			}else{
				$(".ui-sortable").find(".sub-on-item").remove();
			}
		}else{//一级
			if(confirm("将同时删除所有子菜单，是否继续?")){
	　　       $(".ui-sortable").find(".sub-on-item").parent(".box-child").remove();     
			   var r=$(".ui-sortable .box-child").length;  
			   if($(".ui-sortable .box-child:not('.sub-mueu-plusall')").length==2){
				  $(".ui-sortable").append('<div class="box-child sub-mueu-plusall"><div class="sub-js-sortable sub-mueu-plus text-center" onclick="sub_mueu_plus(this)"><i class="icon-plus" ></i>添加菜单</div></div>');      
			   }               
			   $(".ui-sortable .box-child").width(100/r+"%");
			   
			   
			}
		}
		return false;
	}else{
		layer.msg("请选择要删除的菜单",{icon:2});
	}
});
//编辑菜单名
function edietmenuname(obj){
	//判断是否是父级菜单
    var isparent = $(".sub-on-item").parent().is(".box-child");
	//去除值的空格
	var val = $(obj).val().replace(/\s+/g, "");
	//判断是否选中某个菜单
	if($(".sub-on-item").length>0){
		//如果值为空
		if(val==''){
			if(isparent){
				$(".sub-on-item").text('菜单名称');
				$(".sub-on-item").attr("menuname","菜单名称");
			}else{
				$(".sub-on-item").text('子菜单名称');
				$(".sub-on-item").attr("menuname","子菜单名称");
			}
		  
	    }else{
			$(".sub-on-item").text(val);
			$(".sub-on-item").attr("menuname",""+val+"");
		}
	}else{
		layer.msg("请选择菜单",{icon:2});
	}
}
//编辑菜单地址
function edietmenuhref(obj){
	//去除值的空格
	var val = $(obj).val().replace(/\s+/g, "");
	//判断是否选中某个菜单
    if($(".sub-on-item").length>0){
		$(".sub-on-item").attr("menuurl",""+val+"");
	}else{
		layer.msg("请选择菜单",{icon:2});
	}
}
//保存菜单

function savemenu(){
	var index = top.layer.msg('发布中，请稍候',{icon: 16,time:false,shade:0.8});
	$(".menunum").each(function(){
		 var id = $(this).attr("data-id");
		 var pid = $(this).attr("data-pid");
		 var name = $(this).attr("menuname");
		 var url = $(this).attr("menuurl"); 
         console.log("['" + loginname + "','" + loginkey + "','" + id + "','" + pid + "','" + name + "','" + url + "']" );		 
		$.ajax({
			url: "../../../service",
			cache: false,
			async: false,
			data: {
				proc: "c_wxuser.wxmenu_add",
				para: "['" + loginname + "','" + loginkey + "','" + id + "','" + pid + "','" + name + "','" + url + "']"
			},
			dataType: "json",
			success: function(data) {		
				console.log(JSON.stringify(data));
				if(data[0].TAG=='0'){
					top.layer.closeAll();
					top.layer.alert(""+data[0].MSG+"",function(index){
					   top.location.href="login.html";
					   layer.close(index);
					});   
				}else if(data[0].TAG=='2'){
					top.layer.closeAll();
				    layer.msg(""+data[0].MSG+"");
				}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){	
                    top.layer.closeAll();            				
				}else{
                  
				   
				}
				
			}
		});
	});
	top.layer.closeAll();
	wxjson();
	
}
//得到拼接的json
function wxjson(){
	$.ajax({
			url: "../../../service",
			cache: false,
			async: false,
			data: {
				proc: "c_wxuser.wxmenu_json",
				para: "['" + loginname + "','" + loginkey + "']"
			},
			dataType: "json",
			success: function(data) {
				console.log("wxmenu_json==="+JSON.stringify(data));
				var json = data[0].MENUJSON;
				submitmenu(json);
			}
	});
}
//发布威信菜单
function submitmenu(obj){
	console.log("obj==="+obj);
	$.ajax({
			url: "../../../weixinmenu",
			cache: false,
			async: false,
			data: {
				menulist:obj	
			},
			dataType: "json",
			success: function(data) {
				console.log(JSON.stringify(data));
				//var json = data[0].MENUJSON;
			}
	})
}